package com.xiaxueqi.web.portal;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.niagara.context.SecurityContext;
import com.niagara.dao.Page;
import com.niagara.security.auth.entity.UserDetail;
import com.niagara.service.base.BaseService;
import com.niagara.web.struts2.Struts2Utils;
import com.xiaxueqi.entity.book.BookForsaleExt;
import com.xiaxueqi.entity.security.AppUserExt;

/**
 * 用来处理通过在输入框中填写关键词、选择搜索范围的方式的搜索
 * 
 * @author liangThink
 */
public class SearchAction {

	@Autowired
	private BaseService baseService;

	private String key; // 搜索时的关键字
	private String scope; // 搜索的范围：all,book,shop,major (order将来加)
	private Page<BookForsaleExt> bookList; // 利用搜索搜到的书的列表
	private Page<AppUserExt> userList; // 搜索到的用户店铺列表
	private Page page;

	private String loginId = this.setLoginId();

	// 默认会执行的搜索方法
	public String execute() {
		// 调用搜索引擎的方法
		// System.out.println("key is " + key)

		if ("all".equals(scope)) {
			this.searchAllList(key, 10);
			this.setPage();
			return "suceess";
		}
		if ("book".equals(scope)) {
			this.searchBookList(key, 10);
			this.setPage();
			return "suceess";
		}
		if ("shop".equals(scope)) {
			this.searchShopList(key, 10);
			this.setPage();
			return "suceess";
		}
		if ("major".equals(scope)) {
			this.searchMajorList(key, 10);
			this.setPage();
			return "suceess";
		}
		return "suceess";
	}

	private void searchAllList(String key, Integer pageSize) {
		// 搜书
		this.searchBookList(key, pageSize / 2);
		// 搜用户
		Integer pageNo = Struts2Utils.getIntParameter("niagara_params_pageNo",
				Integer.valueOf(1));
		Map param = new HashMap();
		param.put("name", key);
		this.userList = this.baseService.findForPaging(AppUserExt.class, param,
				"select_paging_by_like", pageNo, pageSize / 2);
	}

	private void searchBookList(String key, Integer pageSize) {
		Integer pageNo = Struts2Utils.getIntParameter("niagara_params_pageNo",
				Integer.valueOf(1));
		Map param = new HashMap();
		param.put("bookName", key);
		this.bookList = this.baseService.findForPaging(BookForsaleExt.class,
				param, "select_paging_by_like", pageNo, pageSize);
	}

	private void searchShopList(String key, Integer pageSize) {
		Integer pageNo = Struts2Utils.getIntParameter("niagara_params_pageNo",
				Integer.valueOf(1));
		Map param = new HashMap();
		param.put("name", key);
		this.userList = this.baseService.findForPaging(AppUserExt.class, param,
				"select_paging_by_like", pageNo, pageSize);
	}

	private void searchMajorList(String key, Integer pageSize) {
		Integer pageNo = Struts2Utils.getIntParameter("niagara_params_pageNo",
				Integer.valueOf(1));
		Map param = new HashMap();
		param.put("orgName", key);
		this.userList = this.baseService.findForPaging(AppUserExt.class, param,
				"select_paging_by_like", pageNo, pageSize);
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getKey() {
		return this.key;
	}

	public Page<BookForsaleExt> getBookList() {
		return bookList;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getScope() {
		return scope;
	}

	public Page<AppUserExt> getUserList() {
		return userList;
	}

	public String getLoginId() {
		return loginId;
	}

	public Page getPage() {
		return page;
	}

	private void setPage() {
		if (bookList != null && userList != null) {
			this.page = new Page(bookList.getCurrentPage() >= userList
					.getCurrentPage() ? bookList.getCurrentPage() : userList
					.getCurrentPage(), bookList.getPageSize()
					+ userList.getPageSize(), bookList.getTotalCount()
					+ userList.getTotalCount(), null);
		} else {
			this.page = bookList == null ? (userList != null ? userList : null)
					: bookList;
		}

	}

	private String setLoginId() {
		UserDetail detail = SecurityContext.getUserDetail();
		String tempLoginId = detail != null ? detail.getLoginId() : null;
		return tempLoginId;
	}
}
